package mytest.wd4;

import pojo.LightningResponse;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Test_3 {
    public static void main(String[] args) {
        String DIAL_TASK_REGISTER_SQL = "SELECT\n" +
                "task_id,\n" +
                "create_time,\n" +
                "update_time,\n" +
                "cus_id,\n" +
                "phone,\n" +
                "state,\n" +
                "dial_status,\n" +
                "status_update_time,\n" +
                "achieve_time,\n" +
                "loan_num,\n" +
                "loan_amount\n" +
                "FROM\n" +
                "(\n" +
                "SELECT\n" +
                "a.* \n" +
                "FROM\n" +
                "t_dial_task_%s a\n" +
                "LEFT JOIN t_cus_cat_achieve b ON a.cus_cat = b.cus_cat \n" +
                "AND a.business_id = b.business_id \n" +
                "AND a.user_classify = b.user_classify \n" +
                "WHERE\n" +
                "b.achieve = 0 UNION ALL\n" +
                "SELECT\n" +
                "a.* \n" +
                "FROM\n" +
                "t_dial_task_%s a\n" +
                "LEFT JOIN t_cus_cat_achieve b ON a.cus_cat = b.cus_cat \n" +
                "AND a.business_id = b.business_id \n" +
                "AND a.user_classify = b.user_classify \n" +
                "WHERE\n" +
                "b.achieve = 0 UNION ALL\n" +
                "SELECT\n" +
                "a.* \n" +
                "FROM\n" +
                "t_dial_task_%s a\n" +
                "LEFT JOIN t_cus_cat_achieve b ON a.cus_cat = b.cus_cat \n" +
                "AND a.business_id = b.business_id \n" +
                "AND a.user_classify = b.user_classify \n" +
                "WHERE\n" +
                "b.achieve = 0 \n" +
                ") t \n" +
                "WHERE\n" +
                "phone IN (%s);";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMM");
        String dateStr = "2024-11-02";
        ArrayList<String> phoneList = new ArrayList<>();
        phoneList.add("121212");
        phoneList.add("121213");
        phoneList.add("121214");
        StringBuilder phone = new StringBuilder();
        for (int i = 0; i < phoneList.size(); i++) {
            if (i == phoneList.size() - 1) {
                phone.append("'" + phoneList.get(i) + "'");
            } else {
                phone.append("'" + phoneList.get(i) + "'" + ",");
            }
        }
        LocalDate localDate = LocalDate.parse(dateStr, formatter);
        String currentMonth = localDate.format(formatter2);
        String previousMonth1 = localDate.minusMonths(1).format(formatter2);
        String previousMonth2 = localDate.minusMonths(2).format(formatter2);
        StringBuilder phoneBuilder = new StringBuilder();
        for (int i = 0; i < phoneList.size(); i++) {
            if (i == phoneList.size() - 1) {
                phoneBuilder.append("'" + phoneList.get(i) + "'");
            } else {
                phoneBuilder.append("'" + phoneList.get(i) + "'" + ",");
            }
        }
        String sql = String.format(DIAL_TASK_REGISTER_SQL, currentMonth, previousMonth1, previousMonth2, phoneBuilder);
        System.out.println(sql);
    }
}
